package com.eet.message.config;

import com.eet.message.constants.CommonConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 过期队列TTL
 *
 * @author zheng.zhang
 */
@Configuration
public class TtlMqConfiguration {

    @Bean
    public DirectExchange ttlExchange() {
        return new DirectExchange(CommonConstants.TTL_DIRECT_EXCHANGE, true, false);
    }

    @Bean
    public DirectExchange deadExchange() {
        return new DirectExchange(CommonConstants.DEAD_DIRECT_EXCHANGE, true, false);
    }

    @Bean
    public Queue ttlQueue() {
        Map<String, Object> args = new HashMap<>(4);
        // 设置队列消息过期时间
        args.put(CommonConstants.X_MESSAGE_TTL, 30000);
        // 绑定死信交换机
        args.put(CommonConstants.DEAD_LETTER_EXCHANGE, CommonConstants.DEAD_DIRECT_EXCHANGE);
        // 绑定死信队列路由键
        args.put(CommonConstants.DEAD_LETTER_ROUTING_KEY, CommonConstants.DEAD);
        return new Queue(CommonConstants.TTL_DIRECT_QUEUE, true, false, false, args);
    }

    @Bean
    public Queue deadQueue() {
        return new Queue(CommonConstants.DEAD_DIRECT_QUEUE, true);
    }

    @Bean
    public Binding ttlBinding() {
        return BindingBuilder.bind(ttlQueue())
                .to(ttlExchange())
                .with(CommonConstants.TTL);
    }

    @Bean
    public Binding deadBinding() {
        return BindingBuilder.bind(deadQueue())
                .to(deadExchange())
                .with(CommonConstants.DEAD);
    }
}
